﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;
using Newtonsoft.Json.Linq;
using WebGrease.Css.Extensions;

using CanDoo;
using CanDoo.Core;
using CanDoo.Data;
using CanDoo.Web;
using CanDoo.FineUI;
using CanDoo.Extensions;
using CanDoo.Oil.Core.Models;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Admin.Core.Contracts;
using CanDoo.Core.Data;
using FineUIPro;

namespace CanDoo.Oil.WebApp.storeCallIn
{
    public partial class Default : ListPageBase<ICallInContract, CallIn>
    {


        public IUserContract UserContract { get; set; }
        public IWarehouseContract WarehouseContract { get; set; }

        #region 方法
        protected override void InitGrid()
        {
            Selector = p => new
            {
                p.Code,
                p.InDate,
                p.User,
                p.UserId,
                p.OutWarehouse,
                p.OutWarehouseId,
                p.InWarehouse,
                p.InWarehouseId,
                p.Remark,
                p.PassState,
                p.PassAutitedTime,
                p.PassAutitedUserId,
                p.Id,
                p.Name
            };
            base.InitGrid();
        }

        protected override void Search()
        {
            var key = SearchKey.GetValue();
            var userId = 0L;
            if (SearchUser.Values.Count() > 0 && SearchUser.Values[0].ToLong() >  0L)
                 userId = SearchUser.Values[0].ToLong();
            var outwarehouseId = 0L;
            if (SearchOutWarehouse.Values.Count() > 0 && SearchOutWarehouse.Values[0].ToLong() >  0L)
                 outwarehouseId = SearchOutWarehouse.Values[0].ToLong();
            var inwarehouseId = 0L;
            if (SearchInWarehouse.Values.Count() > 0 && SearchInWarehouse.Values[0].ToLong() >  0L)
                 inwarehouseId = SearchInWarehouse.Values[0].ToLong();
            if(string.IsNullOrEmpty(key) == false)
            {
                Filter = Filter.And(p => p.Name.Contains(key));
            }
            if (userId >  0L)
                Filter = Filter.And(p => p.UserId == userId);
            if (outwarehouseId >  0L)
                Filter = Filter.And(p => p.OutWarehouseId == outwarehouseId);
            if (inwarehouseId >  0L)
                Filter = Filter.And(p => p.InWarehouseId == inwarehouseId);

            var isDeleted = ManageRecycle.Checked;
            Filter = Filter.And(p => p.IsDeleted == isDeleted);

            if (_SearchIsPassed != null && _SearchIsPassed.GetValue() != "-1")
            {
                PassState passstate = PassState.Wait;
                if(Enum.TryParse(_SearchIsPassed.GetValue(),out passstate))
                {
                    Filter = Filter.And(p => p.PassState == passstate);
                }
            }
        }
        #endregion

        #region 事件
        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //FineUIListHelper.BindDDL<User>("发货人",UserContract.GetResolvedTree(), 0L, SearchUser);
                //FineUIListHelper.BindDDL(UserContract.EntityName, this.UserContract.GetSelectList(), 0L, SearchUser);
                //FineUIListHelper.BindDDL<Warehouse>("调出仓库",WarehouseContract.GetResolvedTree(), 0L, SearchWarehouse);
                //FineUIListHelper.BindDDL(WarehouseContract.EntityName, this.WarehouseContract.GetSelectList(), 0L, SearchWarehouse);
                //FineUIListHelper.BindDDL<Warehouse>("调入仓库",WarehouseContract.GetResolvedTree(), 0L, SearchWarehouse);
                //FineUIListHelper.BindDDL(WarehouseContract.EntityName, this.WarehouseContract.GetSelectList(), 0L, SearchWarehouse);
                InitGrid();
            }
        }
        #endregion

        #endregion 事件

    }
}
